﻿using System;
using System.Collections.ObjectModel;
using System.Data;
using DataAccess;
using Entities;

namespace Business
{
    public class DirectorateBusiness
    {
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static DirectorateBusiness Instance { get; private set; }

        /// <summary>
        /// Initializes the <see cref="DirectorateBusiness" /> class.
        /// </summary>
        static DirectorateBusiness() { Instance = new DirectorateBusiness(); }

        /// <summary>
        /// Extracts the raw data.
        /// </summary>
        /// <param name="dataReader">The data reader.</param>
        /// <returns></returns>
        private Directorate ExtractRawData(IDataRecord dataReader)
        {
            return new Directorate
            {
                DirectorateId = new Guid(dataReader["DirectorateID"].ToString()),
                AddressId = new Guid(dataReader["AddressID"].ToString()),
                AddressLine1 = dataReader["AddressLine1"].ToString(),
                AddressLine2 = dataReader["AddressLine2"].ToString(),
                AddressLine3 = dataReader["AddressLine3"].ToString(),
                ContactName = dataReader["FirstName"] + " " + dataReader["SurName"],
                Postcode = dataReader["PostCode"].ToString(),
                Name = dataReader["Name"].ToString(),
                Isactive = (bool)dataReader["IsActive"]
            };
        }

        /// <summary>
        /// Gets the by id.
        /// </summary>
        /// <param name="directorateId">The directorate id.</param>
        /// <returns></returns>
        public Directorate GetById(Guid directorateId)
        {
            if (directorateId == Guid.Empty)
            {
                return null;
            }
            using (var dataReader = DirectorateDataAccess.Instance.GetById(directorateId))
            {
                if (dataReader.Read())
                {
                    return ExtractRawData(dataReader);
                }
                return null;
            }
        }

        /// <summary>
        /// Gets all.
        /// </summary>
        /// <param name="orgId">The org id.</param>
        /// <param name="include">if set to <c>true</c> [include].</param>
        /// <returns></returns>
        public Collection<Directorate> GetAll(Guid orgId,bool include)
        {
            var colDirectorates = new Collection<Directorate>();
            using (var dataReader = DirectorateDataAccess.Instance.GetAll(orgId,include))
            {
                while (dataReader.Read())
                {
                    colDirectorates.Add(ExtractRawData(dataReader));
                }
                return colDirectorates;
            }
        }
    }
}
